﻿using LeafAssist;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PlatformDemo.Demo.Categories
{
    public partial class ReceiveAllCategories : UserControl
    {
        public ReceiveAllCategories()
        {
            InitializeComponent();

            propertyGrid.SelectedObject = new Parameter();

            textBoxBaseUrl.Text = GlobalVariable.BaseUrl;
            textBoxBaseAddr.Text = GlobalVariable.BaseRoute["RetrieveAllCategories"];

            buttonAccess.Click += (object sender, EventArgs eventArgs) =>
            {
                Parameter parameter = propertyGrid.SelectedObject as Parameter;

                var httpRequest = new HttpRequest();

                var urlParams = new RequestParams
                {
                    ["limit"] = parameter.limit,
                    ["page"] = parameter.page,
                    ["fields"] = parameter.fields,
                };

                string url = string.Format("{0}{1}", textBoxBaseUrl.Text, textBoxBaseAddr.Text);

                httpRequest.UserAgent = Http.FirefoxUserAgent();

                if (GlobalVariable.Token == null)
                {
                    string message = "当前令牌为空！\n请先获取令牌！！！";
                    string caption = "错误";
                    MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                    DialogResult result;

                    result = MessageBox.Show(this, message, caption, buttons, MessageBoxIcon.Error);
                    return;
                }

                httpRequest.Authorization = $"{GlobalVariable.Token.token_type} {GlobalVariable.Token.access_token}";

                new Thread(Action =>
                {
                    Application.DoEvents();
                    textBoxResult.Invoke(new Action(() =>
                    {
                        HttpResponse httpResponse = httpRequest.Get(url, urlParams);

                        switch (httpResponse.StatusCode)
                        {
                            case HttpStatusCode.OK:
                                labelHttpStatus.ForeColor = Color.Green;
                                textBoxResult.Text = httpResponse.ToString();
                                break;
                            default:
                                labelHttpStatus.ForeColor = Color.Red;
                                break;
                        }

                        labelHttpStatus.Text = $"响应码: {(int)httpResponse.StatusCode}";

                    }));
                }).Start();
            };
        }

        public class Parameter
        {
            [Category("分页")]
            [Description("结果数量（默认值：50）（最大值：250）")]
            [DisplayName("结果数量")]
            public int limit { get; set; } = 50;

            [Category("分页")]
            [Description("要显示的页面（默认值：1）")]
            [DisplayName("显示页面")]
            public int page { get; set; } = 1;

            [Category("列名")]
            [Description("以逗号分隔的要包含在响应中的字段列表")]
            [DisplayName("返回列项")]
            public string fields { get; set; } = "id,name,parent_category_id";
        }
    }

    public class ReceiveAllCategoriesInterface : Interface
    {
        public string Name => "检索分类";

        public string Category => "分类";

        public Control CreatePanel()
        {
            return new ReceiveAllCategories();
        }
    }
}
